package dry.example.gui;

import javax.swing.*;
import java.awt.*;

public class BoxLayoutDemo {
    public static void main(String[] args) {
        // 创建主窗口
        JFrame frame = new JFrame("BoxLayout 示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 300);

        // 创建一个垂直排列的 JPanel
        JPanel verticalPanel = new JPanel();
        verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));
        verticalPanel.setBorder(BorderFactory.createTitledBorder("垂直布局"));

        // 添加按钮并设置间距
        verticalPanel.add(new JButton("按钮 1"));
        verticalPanel.add(Box.createRigidArea(new Dimension(0, 10))); // 垂直间距
        verticalPanel.add(new JButton("按钮 2"));
        verticalPanel.add(Box.createVerticalGlue()); // 弹簧填充剩余空间
        verticalPanel.add(new JButton("按钮 3"));

        // 创建一个水平排列的 JPanel
        JPanel horizontalPanel = new JPanel();
        horizontalPanel.setLayout(new BoxLayout(horizontalPanel, BoxLayout.X_AXIS));
        horizontalPanel.setBorder(BorderFactory.createTitledBorder("水平布局"));

        // 添加按钮并设置间距
        horizontalPanel.add(new JButton("选项 A"));
        horizontalPanel.add(Box.createHorizontalGlue()); // 水平弹簧
        horizontalPanel.add(new JButton("选项 B"));
        horizontalPanel.add(Box.createRigidArea(new Dimension(10, 0))); // 水平间距
        horizontalPanel.add(new JButton("选项 C"));

        // 将两个面板放入主容器中
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.add(verticalPanel);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        mainPanel.add(horizontalPanel);

        // 设置窗口内容
        frame.add(mainPanel);
        frame.setVisible(true);
    }
}
